//
//  TBCScrollView.m
//  待定
//
//  Created by BoyaLee on 15/11/6.
//  Copyright © 2015年 BoyaLee. All rights reserved.
//

#import "TBCScrollView.h"

@interface TBCScrollView () <UIScrollViewDelegate>

@property (strong, nonatomic) NSMutableArray *array;
@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic) UIPageControl *pageControl;
@property (strong, nonatomic) NSTimer *timer;

@end

@implementation TBCScrollView

- (instancetype)initWithArrayImages:(NSMutableArray *)array PlaceHolderImageName:(NSString *)placeholderimageName {
    
    if (self = [super init]) {
        
        self.array = [NSMutableArray arrayWithCapacity:0];
        [array addObject:array.firstObject];
        [array insertObject:array.lastObject atIndex:0];
        self.array = array;
        [self initScrollViews:placeholderimageName];
        
    }
    
    return self;
    
}

- (void)initScrollViews:(NSString *)imageName {
    
    self.frame = CGRectMake(0, 0, WIDTH, (HEIGHT - 64) * 0.4);
    self.backgroundColor = [UIColor lightGrayColor];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, (HEIGHT - 64) * 0.4)];
    self.scrollView.contentSize = CGSizeMake(WIDTH * self.array.count, (HEIGHT - 64) * 0.4);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.delegate = self;
    [self addSubview:self.scrollView];
    
    for (int i = 0; i < self.array.count; i++) {
        
        NSString *img = self.array[i];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, (HEIGHT - 64) * 0.4)];
        imageView.backgroundColor = [UIColor lightGrayColor];
        [imageView sd_setImageWithURL:[NSURL URLWithString:img] placeholderImage:[UIImage imageNamed:imageName]];
        [self.scrollView addSubview:imageView];
        
    }
    self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
    
    UIView * viewFooter = [[UIView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height - 30, WIDTH, 30)];
    [viewFooter setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.1]];
    [self addSubview:viewFooter];
    
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake((WIDTH - 30) / 2, 5, 30, 20)];
    self.pageControl.numberOfPages = self.array.count - 2;
    self.pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    self.pageControl.pageIndicatorTintColor = [UIColor whiteColor];
    [self.pageControl addTarget:self action:@selector(pageControlMethod) forControlEvents:UIControlEventValueChanged];
    [viewFooter addSubview:self.pageControl];
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(headLineScrollTimer) userInfo:nil repeats:YES];
    
}

- (void)pageControlMethod {
    
    self.scrollView.contentOffset = CGPointMake((self.pageControl.currentPage + 1) * self.scrollView.bounds.size.width, 0);
    
}

#pragma mark -- 当手指接触的时候让定时器停止
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    
    [self.timer invalidate];
    self.timer = nil;
    
}

#pragma mark -- 当手指离开的时候开始滚动
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(headLineScrollTimer) userInfo:nil repeats:YES];
    
}

#pragma mark -- 定时器在不断循环滚动，就不会减速停止，此方法就不会走
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    if (self.scrollView.contentOffset.x >= self.scrollView.bounds.size.width * (self.array.count - 1)) {
        
        [self.scrollView scrollRectToVisible:CGRectMake(self.scrollView.bounds.size.width, 0, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height) animated:NO];
        
    }else if (self.scrollView.contentOffset.x <= 0) {
        
        [self.scrollView scrollRectToVisible:CGRectMake(self.scrollView.contentSize.width - 2 * self.scrollView.bounds.size.width, 0, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height) animated:NO];
        
    }
    
}

#pragma mark -- 滑动滚动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    self.pageControl.currentPage = (self.scrollView.contentOffset.x - self.scrollView.bounds.size.width) / self.scrollView.bounds.size.width;
    
}

#pragma mark -- 定时器
- (void)headLineScrollTimer {
    
    double x = self.scrollView.contentOffset.x;
    self.scrollView.contentOffset = CGPointMake(x + self.scrollView.bounds.size.width, 0);
    if (self.scrollView.contentOffset.x == 0) {
        
        [self.scrollView scrollRectToVisible:CGRectMake(self.scrollView.contentSize.width - 2 * self.scrollView.bounds.size.width, 0, self.scrollView.bounds.size.width, self.scrollView.bounds.size.width) animated:NO];
        
    }else if (self.scrollView.contentOffset.x == self.scrollView.contentSize.width - self.scrollView.bounds.size.width) {
        
        [self.scrollView scrollRectToVisible:CGRectMake(self.scrollView.bounds.size.width, 0, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height) animated:NO];
        
    }
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
